package com.schedule.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.schedule.dao.CompanyDao;
import com.schedule.model.Company;
import com.schedule.model.Place;


@Controller
@RequestMapping("/company")
public class CompanyController {
	@Autowired
	private CompanyDao companyDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getAll(HttpSession session){
		ModelAndView mav=new ModelAndView("company/CompanyList");
		List<Company> companys=companyDao.findAll();
		System.out.println(companys.size());
		mav.addObject("companys1",companys);
		String adminName=session.getAttribute("adminName").toString(); 
		 mav.addObject("name",adminName);
		return mav;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(
			@RequestParam String companyId
			){
			companyDao.deleteCompany(companyId);
		ModelAndView mav=new ModelAndView("redirect:/company");
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public ModelAndView update(
			@RequestParam int companyId
			){
		Company company=companyDao.findById(companyId);
		ModelAndView mav=new ModelAndView("company/updateCompany");
		mav.addObject(company);
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(
			@RequestParam int companyId,
			@RequestParam String name
			){
		Company company2=new Company();
		company2.setCompanyId(companyId);
		company2.setName(name);
		companyDao.updateCompany(company2);
		ModelAndView mav=new ModelAndView("redirect:/company");
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("company/addCompany");
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(
			@RequestParam int Id,
			@RequestParam String name
			){
		Company company=new Company();
		company.setCompanyId(Id);
		company.setName(name);
		companyDao.addCompany(company);
		ModelAndView mav=new ModelAndView("redirect:/company");
		return mav;
	}
}
